import { getGoods,deleteGoods,postAddGoods,getGoodsId} from '@/api/goods'
const state={
    //发送请求的参数
    pagination:{
        query:'',
        //当前页码	
        pagenum:1,
        //每页显示条数
        pagesize:5,
    },
    //响应后取到的pagination数据
    paginationData:{
        //总页数
        total:null,
        //所有的商品数据
        goods:[]
    },
    //当前行的值
    currentRow:{},
    //当前的选中的步骤条
    cuurent_steps_active:0,
    //添加商品所需要的参数
    addGoodsQuery:{
        goods_name:'',
        goods_cat:'',
        goods_price:'',
        goods_number:'',
        goods_weight:'',
        goods_introduce:'',
        pics:[],
        attrs:[]
    },
    //商品参数
    goods_params:[],
    //商品属性
    goods_attrs:[],
    //params的页面数据
    params_data:[],

};
const getters={
    //获取attr_vals并且转化成数组
    get_attr_vals(state,getters,root){
        const attr_vals_arr =state.goods_params.map(item=>item.attr_vals);
        for(let i=0;i<attr_vals_arr.length;i++){
            return attr_vals_arr[i].split(' ');
        }
        //attr_vals_arr.forEach( (item,index) => { item.split(' ')})
        
    }, 
    //获取attr_vals和id
    get_attr_valsAnd_id(state){
        function filterObject(arr,keyName1,keyName2){
            return arr.map(item=>{
                    let tmp=JSON.parse(JSON.stringify(item));
                    for(let key in tmp){
                        if(key!==keyName1&&key!==keyName2) delete tmp[key]
                    }
                    return tmp;}
            )
        }

        let newArr=[];
        let lastArr=newArr.concat(filterObject(state.goods_params,'attr_id','attr_vals')).concat(
            filterObject(state.goods_attrs,'attr_id','attr_vals'))
        return lastArr;
    },
    //获取good_cat
    get_good_cat_length(state){
        return state.addGoodsQuery.goods_cat.split(',')
    } 

};
const mutations={
   //修改参数pagenum
    CHANGE_PAGENUM(state,playLoad){
        state.pagination.pagenum=playLoad;
    },
   //修改参数pagesize
    CHANGE_PAGESIZE(state,playLoad){
        state.pagination.pagesize=playLoad;
    },
  //修改参数query
    CHANGE_QUERY(state,playLoad){
    state.pagination.query=playLoad;
    },
  //修改获取到的pagination的total数据
    CHANGE_TOTAL(state,playLoad){
    state.paginationData.total=playLoad;
    },
  //修改获取到的pagination的goods数据
    CHANGE_GOODS(state,playLoad){
    state.paginationData.goods=playLoad;
    },
  //修改当前行
    CHANGE_CURRENTROW(state,playLoad){
    state.currentRow=playLoad;
    },
    //存储获取到的pagination的goods数据
    ADD_USERS(state,playLoad){
        state.paginationData.goods.push(playLoad);
    },
    //修改当前步骤条选中的值
    CHANGE_STEPS_ACTIVE(state,playLoad){
        state.cuurent_steps_active=playLoad
    },
    //存储添加商品所需要的参数addGoodsQuery
    CHANGE_ADDGOODSQUERY(state,playLoad){
    console.log("添加商品所需要的参数是",playLoad)
    if(playLoad instanceof Object){
        for(const key in playLoad){
            state.addGoodsQuery[key]=playLoad[key]
        }
    }else{
        return console.log('参数不是一个对象');
    }
    },
    //修改商品参数
    CHANGE_GOODS_PARAMS(state,playLoad){
        state.goods_params=playLoad
    },
    //添加商品参数
    CHANGE_GOODS_ATTRS(state,playLoad){
        state.goods_attrs=playLoad
    },
    //params的页面数据
    CHANGE_PARAMS_DATA(state,playLoad){
        state.params_data=playLoad;
    },



};
const actions={
    //异步获取商品列表
    Acs_getGoods(content,playload){
        return new Promise((resolve,reject)=>{
             content.commit('CHANGE_QUERY','')   
             getGoods({...content.state.pagination})
             .then(res=>{
                let {total,goods}=res;
                //console.log(users);
                content.commit('CHANGE_TOTAL',total);
                content.commit('CHANGE_GOODS',goods);
                 resolve(res)
             })
             .catch(err=>{
                 reject(err) 
             })
        })     
    },
    //异步删除商品
    Acs_deleteUser(content,playload){
        //console.log(content.state.currentRow);
        return new Promise((resolve,reject)=>{
            deleteGoods(content.state.currentRow.goods_id)
            .then(res=>{
                let {goods}=content.state.paginationData
                goods.splice(goods.indexOf(content.state.currentRow),1)
                resolve(res)

            })
            .catch(err=>{
                reject(err) 
            })
       })     
    },
    //异步添加商品
    Acs_postAddGoods(content,playload){
        return new Promise((resolve,reject)=>{
            postAddGoods(content.state.addGoodsQuery)
            .then(res=>{
                resolve(res);
            })
            .catch(err=>{
                reject(err)
            })
        })
    },
    //异步查询商品id
    Acs_getGoodsId(content,playload){
        return new Promise((resolve,reject)=>{
            getGoodsId({...content.state.pagination})
            .then(res=>{
                content.commit('CHANGE_GOODS',res.goods)
                resolve(res);
            })
            .catch(err=>{
                reject(err)
            })
        })
    },


};
export default {
    namespaced:true,
    state,
    getters,
    mutations,
    actions
}